<script lang="ts">
export default {
  name: "RepoCompEditDialog"
}
</script>

<!--
 * @Description: 编辑对话框
 * @version:
 * @Author:
 * @Date: 2022-10-24 09:10:28
 * @LastEditors:
 * @LastEditTime: 2022-10-24 09:10:28
-->
<template>
  <!-- 对话框 -->
  <el-dialog :close-on-click-modal="false" :model-value="visible" :title="dataState.title" min-width="200px"
             @close="handleCancel">

    <!-- 数据表单 -->
    <el-form ref="dataFormRef" :model="dataState.formData" :rules="dataState.rules" label-width="80px">

      <!-- 编辑框：制造厂商 -->
      <el-form-item label="制造厂商" prop="manufacturer">
        <el-input v-model="dataState.formData.manufacturer" :disabled="dataState.type==='update'"/>
      </el-form-item>

      <!-- 编辑框：设备类型 -->
      <el-form-item label="设备类型" prop="deviceType">
        <el-input v-model="dataState.formData.deviceType" :disabled="dataState.type==='update'"/>
      </el-form-item>

    </el-form>

    <!-- 脚部：确认/取消按钮-->
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="handleCancel">取 消</el-button>
      </div>
    </template>

  </el-dialog>
</template>

<script lang="ts" setup>
import {nextTick, onMounted, reactive, ref} from "vue";

import {ElForm} from "element-plus";
import {DeviceOperateItem} from "@/api/repo/comp/local/types";
import {CreateRepoCompRequestVO} from "@/api/repo/comp/local/types";

const props = defineProps(['visible']);
const emit = defineEmits(['create', 'update', 'cancel']); //注意：这个是父组件<dict-type-edit>标签上的@close
const dataFormRef = ref(ElForm); // 属性名必须和元素的ref属性值一致

/**
 * 对外声明接口：父页面可以调用它
 */
defineExpose({initEditData});

// 数据状态：定义
let dataState = reactive({
  // 类型：create/update
  type: 'create',
  // 标题
  title: '添加设备',
  // 表单数据
  formData: {
    id: 0,
    compRepo: '',
    compType: '',
    deviceType: '',
    manufacturer: '',
  },

  // 对话框的各控件输入规则
  rules: {
    deviceType: [{required: true, message: '设备类型不能为空', trigger: 'blur'}],
    manufacturer: [{required: true, message: '制造厂商不能为空', trigger: 'blur'}],
  },
});

function handleCancel() {
  emit('cancel', "子组件传给父组件的值");
}

function handleSubmit() {
  let formData = {
    id: undefined,
    compRepo: '',
    compType: '',
    compParam: {
      deviceType: '',
      manufacturer: '',
    },
  } as CreateRepoCompRequestVO;

  formData.id = dataState.formData.id;
  formData.compRepo = dataState.formData.compRepo;
  formData.compType = dataState.formData.compType;
  formData.compParam.manufacturer = dataState.formData.manufacturer;
  formData.compParam.deviceType = dataState.formData.deviceType;

  if (dataState.type === 'create') {
    formData.id = undefined;
    emit('create', formData);
  }
  if (dataState.type === 'update') {
    emit('update', formData);
  }
}

function initEditData(type: string, entity: DeviceOperateItem) {
  if (type === 'create') {
    dataState.type = type;
    dataState.title = '新增配置';

    dataState.formData.id = 0;
    dataState.formData.compRepo = 'local';
    dataState.formData.compType = 'jsp-decoder';
    dataState.formData.deviceType = '';
    dataState.formData.manufacturer = '';

    nextTick(() => {
      dataFormRef.value.resetFields();
    })

    return;
  }

  if (type === 'update') {
    dataState.type = type;
    dataState.title = '修改配置';

    dataState.formData.id = entity.id as number;

    return;
  }
}

/**
 * 响应页面安装：页面的初始化工作
 */
onMounted(() => {
});

</script>
